package com.wjk.kylin.core.common.oss.fastdfs;

import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.wjk.kylin.config.properties.SysProperties;
import com.wjk.kylin.core.common.oss.AbstractOSSServer;
import com.wjk.kylin.core.exception.BusinessException;
import com.wjk.kylin.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@Component
public class FastDFSServer extends AbstractOSSServer<FastDFSFileModel> {
    @Autowired
    private SysProperties sysProperties;

    @Autowired
    private FastFileStorageClient storageClient;

//    @Value("${sys.setting.accessUrl}")
//    private String accessUri;

    @Override
    protected String upload(FastDFSFileModel model) {
        Set<MetaData> metaDataSet = new HashSet<>();
        metaDataSet.add(new MetaData("Author", model.getAuth()));
        metaDataSet.add(new MetaData("CreateDate", DateUtils.getDate()));

        try {
            StorePath storePath = storageClient.uploadFile(model.getFile().getInputStream(), model.getFile().getSize(), model.getExtName(), metaDataSet);
            return sysProperties.getAccessUrl() + storePath.getFullPath();
        } catch (Exception e){
            log.error("上传文件失败",e);
            throw new BusinessException("上传文件失败，请检查配置信息");
        }
    }

    @Override
    protected void delete(String filePath) {

    }

    @Override
    protected File download(String filePath) {
        return null;
    }
}
